package com.tanhua.dubbo.api;

import cn.hutool.core.collection.CollUtil;
import com.tanhua.model.mongo.UserLocation;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.geo.Circle;
import org.springframework.data.geo.Distance;
import org.springframework.data.geo.Metrics;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.geo.GeoJsonPoint;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import java.util.List;

@DubboService
public class UserLocationApiImpl implements UserLocationApi{

    @Autowired
    private MongoTemplate mongoTemplate;



    //更新地理位置
    @Override
    public Boolean updateLocation(Long userId, Double longitude, Double latitude,String address) {
       try{
        //根据用户id查询其地理位置
        Query query =Query.query(Criteria.where("userId").is(userId));
        UserLocation location = mongoTemplate.findOne(query, UserLocation.class);
        if(location == null){
            //如果没有信息，则保存
            location = new UserLocation();
            location.setUserId(userId);
            location.setAddress(address);
            location.setCreated(System.currentTimeMillis());
            location.setUpdated(System.currentTimeMillis());
            location.setLastUpdated(System.currentTimeMillis());
            location.setLocation(new GeoJsonPoint(longitude,latitude));
            mongoTemplate.save(location);

        }else{
            //如果有信息。则更新
            Update update =Update.update("location",new GeoJsonPoint(longitude,latitude))
                    .set("updated",System.currentTimeMillis())
                    .set("lastUpdated",location.getUpdated());
            mongoTemplate.updateFirst(query,update,UserLocation.class);
        }
        return true;
    } catch (Exception e) {
        e.printStackTrace();
        return false;
    }
    }

    //查找附近的人所有的用户的id
    @Override
    public List<Long> queryNearUser(Long userId, Double metre) {
        //根据用户id，查询用户的位置信息
        Query query =Query.query(Criteria.where("userId").is(userId));
        UserLocation location = mongoTemplate.findOne(query, UserLocation.class);
        if(location == null){
            return null;
        }
        //以用户为原点
        GeoJsonPoint point=location.getLocation();
        //绘制半径
        Distance distance=new Distance(metre / 1000, Metrics.KILOMETERS);
        //绘制圆形
        Circle circle=new Circle(point ,distance);
        //查询
        Query locationQuery=Query.query(Criteria.where("location").withinSphere(circle));
        List<UserLocation> userLocations = mongoTemplate.find(locationQuery, UserLocation.class);

        return CollUtil.getFieldValues(userLocations,"userId",Long.class);
    }

}
